package com.itheima.day10;

import com.itheima.domain.Student;
import com.itheima.util.NameUtil;

import java.time.LocalDate;
import java.util.*;

public class Ex3 {
    private static Random r = new Random();
    public static void main(String[] args) {
        LinkedList<Student> list = new LinkedList<>();
        for (int i = 0; i < 50; i++) {
            Student s = new Student();
            list.add(s);
        }

        getSid(list);
        getName(list);
        getSex(list);
        getBirthday(list);
        getAge(list);
        getGroup(list);
        getPhone(list);
        getWx(list);
        for (Student student : list) {
            System.out.println(student);
        }
//男生和女生各自有多少人？
        int man=0;
        int women=0;
        for (int i = 0; i < list.size(); i++) {
            Student student = list.get(i);
            String sex = student.getGender();
            if(sex.equals("男")){
                man++;
            }else{
                women++;
            }
        }
        System.out.println("男生人数:"+man);
        System.out.println("女生人数:"+women);
//00后有多少人？
        int num0=0;
        for (int i = 0; i < list.size(); i++) {
            Student student = list.get(i);
            int year = student.getBirthday().getYear();
            if(year>=2000){
                num0++;
            }
        }
        System.out.println("00后有"+num0+"人");
//名字长度是2的学生有多少人？
//名字长度是3的学生有多少人？
//姓张的同学有多少人？
        int name2=0;
        int name3=0;
        int zhang=0;
        for (int i = 0; i < list.size(); i++) {
            Student student = list.get(i);
            String name = student.getName();
            if(name.length()==2){
                name2++;
            }else if(name.length()==3){
                name3++;
            }
            if(name.charAt(0)=='张'){
                zhang++;
            }

        }
        System.out.println("名字长度是2的学生有"+name2+"人");
        System.out.println("名字长度是3的学生有"+name3+"人");
        System.out.println("姓张的学生有"+zhang+"人");
//手机号中至少包含2个6的有哪些？
//手机号中至少包含1个6和1个8的有哪些？
        System.out.println("手机号中至少包含2个6的学生有:");
        for (int i = 0; i < list.size(); i++) {
            Student student = list.get(i);
            String phone = student.getPhone();
            int num6=0;
            for (int j = 0; j < phone.length(); j++) {
                char c = phone.charAt(j);
                if(c=='6'){
                    num6++;
                }
            }
            if(num6>=2){
                System.out.println(student);
            }
        }
        System.out.println("手机号中至少包含1个6和1个8的学生有:");
        for (int i = 0; i < list.size(); i++) {
            Student student = list.get(i);
            String phone = student.getPhone();
            int num6=0;
            int num8=0;
            for (int j = 0; j < phone.length(); j++) {
                char c = phone.charAt(j);
                if(c=='6'){
                    num6++;
                }
                if(c=='8'){
                    num8++;
                }
            }
            if(num6>=1&&num8>=1){
                System.out.println(student);
            }
        }
//有2个的微信号的人个数
//有1个的微信号的人个数
        int wx1=0;
        int wx2=0;
        for (int i = 0; i < list.size(); i++) {
            Student student = list.get(i);
            String wx = student.getWxs().toString();
            if(wx.length()>10){
                wx2++;
            }else{
                wx1++;
            }
        }
        System.out.println("有2个的微信号的人个数:"+wx2);
        System.out.println("有1个的微信号的人个数:"+wx1);
//各个小组的平均年龄？
//最大三个年纪的
//最小三个年纪的
//最大年纪的人和最小年纪的人是谁，差多久?
        for (int i = 1; i <= 10; i++) {
            TreeSet<Student> groupStudents = new TreeSet<>(new Comparator<Student>() {
                @Override
                public int compare(Student o1, Student o2) {
                    int number=o1.getAge()-o2.getAge();
                    int result=number==0?o1.getName().compareTo(o2.getName()):number;
                    return result;
                }
            });
            int ageSum=0;
            int count=0;

            for (int j = 0; j < list.size(); j++) {
                Student student = list.get(j);
                int group = student.getGroup();
                int age = student.getAge();
                if(group==i){
                    groupStudents.add(student);
                    ageSum+=age;
                    count++;
                }
            }
            System.out.println("第"+i+"组学生平均年龄为:"+ageSum/count);
            Student[] stuArr = groupStudents.toArray(new Student[count]);
            System.out.println("最小三个年纪的学生:"+stuArr[0]+" "+stuArr[1]+" "+stuArr[2]);
            System.out.println("最大三个年纪的学生:"+stuArr[stuArr.length-1]+"  "+stuArr[stuArr.length-2]+"  "+stuArr[stuArr.length-3]);
            System.out.println("最大年纪的学生:"+stuArr[stuArr.length-1]);
            System.out.println("最小年纪的学生:"+stuArr[0]);
            System.out.println("年纪最大和最小学生差"+(stuArr[stuArr.length-1].getAge()-stuArr[0].getAge())+"岁");


        }
//不同的姓各自有多少学生？
        ArrayList<String> danList = NameUtil.danXing();
        ArrayList<String> fuList = NameUtil.fuXing();
        for (int j = 0; j < danList.size(); j++) {
            String danxing = danList.get(j);
            int numDan=0;
            for (int i = 0; i < list.size(); i++) {
                Student student = list.get(i);
                 String dan = student.getName().charAt(0) + "";
                if (danxing.equals(dan)) {
                    numDan++;
                }
            }
            System.out.println("姓" + danxing + "的学生有" + numDan + "个");

        }
        for (int m = 0; m < fuList.size(); m++) {
            String fuXing = fuList.get(m);
            int numFu=0;
            for (int i = 0; i < list.size(); i++) {
                Student student = list.get(i);
                String fu = student.getName().charAt(0)+ "" + student.getName().charAt(1) ;
                if (fuXing.equals(fu)) {
                    numFu++;
                }
            }
            System.out.println("姓" + fuXing + "的学生有" + numFu + "个");
        }
//不同的年龄段有多少人？
        //1990-2020
        int num90=0;
        int num00=0;
        int num10=0;
        for (int i = 0; i < list.size(); i++) {
            Student student = list.get(i);
            int year = student.getBirthday().getYear();
            if(year-1990<10){
                num90++;
            }else if(year-1990<20){
                num00++;
            }else{
                num10++;
            }
        }
        System.out.println("90后有"+num90+"人");
        System.out.println("00后有"+num00+"人");
        System.out.println("10后有"+num10+"人");
    }



//获取学号
    public static void getSid(LinkedList<Student> list ) {
        String sid=null;
        for (int i = 1; i <= 50; i++) {
            if(i<10){
                sid="s00"+i;
            }else{
                sid="s0"+i;
            }
            Student stu = list.get(i - 1);
            stu.setSid(sid);
        }
    }
//获取姓名
    public static void getName(LinkedList<Student> list ) {
        String name=null;
        for (int i = 1;i<=50;i++) {
            //获取单姓
            ArrayList<String> danXinglist = NameUtil.danXing();
            int danIndex = r.nextInt(danXinglist.size());
            String dan = danXinglist.get(danIndex);
            //获取fuXing
            ArrayList<String> fuXingList = NameUtil.fuXing();
            int fuIndex = r.nextInt(fuXingList.size());
            String fu = fuXingList.get(fuIndex);
            //获取字
            ArrayList<String> ziList = NameUtil.getZi();
            int ziIndex = r.nextInt(ziList.size());
            String zi = ziList.get(ziIndex);

            int num = r.nextInt(2);
            if(num==0){
                name=dan+zi;
            }else{
                name=fu+zi;
            }
            Student stu = list.get(i - 1);
            stu.setName(name);
        }
    }
//获取性别
    public static void getSex(LinkedList<Student> list) {
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            int num = r.nextInt(2);
            if(num==0){
                stu.setGender("男");
            }else{
                stu.setGender("女");
            }
        }

    }
//获取生日
    public static void getBirthday(LinkedList<Student> list) {

        for (int i = 0; i < list.size(); i++) {
            //随机年,月,日
            int year = r.nextInt(31)+1990;//1990-2020
            int month = r.nextInt(12)+1;
            int day=0;
            if(month==1||month==3||month==5||month==7||month==8||month==10||month==12){
                day = r.nextInt(31)+1;
            }
            if(month==4||month==6||month==9||month==11){
                day = r.nextInt(30)+1;
            }
            if (month == 2) {
                if(year%4==0&&year%100==0||year%400==0){
                    day = r.nextInt(29)+1;//1-29
                }else{
                    day = r.nextInt(28)+1;//1-28
                }

            }
            LocalDate birthday = LocalDate.of(year, month, day);
            Student stu = list.get(i);
            stu.setBirthday(birthday);
        }
    }
//获取年龄
    public static void getAge(LinkedList<Student> list) {
        LocalDate now = LocalDate.now();
        int nowYear = now.getYear();
        int nowMonth = now.getMonthValue();
        int dayOfMonth = now.getDayOfMonth();
        int age;
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            LocalDate birthday = stu.getBirthday();
            int birthdayYear = birthday.getYear();
            int birthdayMonth = birthday.getMonthValue();
            int birthdayDay = birthday.getDayOfMonth();
            if(nowMonth>=birthdayMonth&&dayOfMonth>birthdayDay){
                age = nowYear - birthdayYear;
                stu.setBirthday(birthday);
            }else{
                age=nowYear - birthdayYear-1;
                stu.setBirthday(birthday);
            }
        }

    }
//获取组编号
    public static void getGroup(LinkedList<Student> list) {
        for (int i = 0; i < list.size(); i++) {
            if(i>=0&&i<5){
                Student stu = list.get(i);
                stu.setGroup(1);
            }else if(i>=5&&i<10){
                Student stu = list.get(i);
                stu.setGroup(2);
            }else if(i>=10&&i<15){
                Student stu = list.get(i);
                stu.setGroup(3);
            }else if(i>=15&&i<20){
                Student stu = list.get(i);
                stu.setGroup(4);
            }else if(i>=20&&i<25){
                Student stu = list.get(i);
                stu.setGroup(5);
            }else if(i>=25&&i<30){
                Student stu = list.get(i);
                stu.setGroup(6);
            }else if(i>=30&&i<35){
                Student stu = list.get(i);
                stu.setGroup(7);
            }else if(i>=35&&i<40){
                Student stu = list.get(i);
                stu.setGroup(8);
            }else if(i>=40&&i<45){
                Student stu = list.get(i);
                stu.setGroup(9);
            }else if(i>=45&&i<50){
                Student stu = list.get(i);
                stu.setGroup(10);
            }
        }
    }
//获取手机号
    public static void getPhone(LinkedList<Student> list) {

        for (int i = 0; i < list.size(); i++) {
            String phone=1+"";
            for (int j = 0; j < 10; j++) {
                int num = r.nextInt(10);
                phone+=num;
            }
            Student stu = list.get(i);
            stu.setPhone(phone);
        }


    }
//获取微信号
    //生成随机数,0----一个微信号     1------两个微信号
    public static void getWx(LinkedList<Student> list) {
        for (int m = 0; m < list.size(); m++) {
            int i = r.nextInt(2);
            Student student = list.get(m);

            String s="";
            String s1="";
            String s2="";
            if(i==0){
                //0----一个微信号  微信号由10个数字组成
                for (int j = 1; j <= 10; j++) {
                    int num = r.nextInt(10);
                     s+=num;
                }
                student.setWxs(new ArrayList<String>(List.of(s)));

            }else{
                for (int k = 1; k <= 10; k++) {
                    int num = r.nextInt(10);
                    s1+=num;
                }
                for (int k = 1; k <= 10; k++) {
                    int num = r.nextInt(10);
                    s2+=num;
                }
                student.setWxs(new ArrayList<String>(List.of(s1,s2)));

            }


        }



    }



}